<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="cate in cates" :key="cate.id">
        <img :src="cate.icon" alt="">
        <h5>{{cate.name}}</h5>
      </li>
    </ul>
    <ul>
      <li v-for="item in items" :key="item.id">
        <img :src="item.pic" alt="">
        <h5>{{item.name}}</h5>
      </li>
    </ul>
  </div>
  <script src="./vue.js"></script>
  <script src="./axios.js"></script>
  <script>

    // 创建axios实例
    /*  const request = axios.create({
       baseURL: 'https://api.it120.cc/conner',
       timeout: 5000
     }) */
    axios.defaults.baseURL = 'https://api.it120.cc/conner';
    axios.defaults.timeout = 5000;

    // 添加一个请求拦截器
    axios.interceptors.request.use(function (config) {
      // 请求之前 可以 添加一些配置 比如 添加请求头，比如传入一些请求参数等
      console.log(config, '请求拦截');
      config.headers.token = '1111';
      if(config.method === 'post') {
        config.data.c = 100;
      }
      if(config.method === 'get') {
        config.params.c = 200;
      }
      return config; // 放行
    }, function (error) {
      // 请求错误的拦截
      return Promise.reject(error);
    });

    // 添加一个相应拦截
    axios.interceptors.response.use(function (response) {
      // 响应成功拦截 http状态码是 2xx
      return response;
    }, function (error) {
      // 响应失败拦截 http状态码不是 2xx
      return Promise.reject(error);
    });

    const vm = new Vue({
      el: '#app',
      data: {
        cates: [],
        items: []
      },
      created() {
        this.fetchCates();
        this.fetchItems();
      },
      methods: {
        fetchCates() {
          axios.get('/shop/goods/category/all', {
            params: {
              a: 10,
              b: 20
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.cates = res.data.data
            }
          })
        },
        fetchItems() {
          axios.post('/shop/goods/list/v2', {
            a: 10,
            b: 20
          }).then(res => {
            if (res.data.code === 0) {
              this.items = res.data.data.result
            }
          })
        }
      }
    })
  </script>
</body>

</html>